package org.example.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.entity.Aircraft;
import org.example.entity.Area;
import org.example.entity.Time;
import org.example.exception.MyException;
import org.example.mapper.AircraftMapper;
import org.example.mapper.AirlineMapper;
import org.example.mapper.AreaMapper;
import org.example.service.AircraftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AircraftServiceImpl extends ServiceImpl<AircraftMapper, Aircraft> implements AircraftService {
    @Autowired
    private AircraftMapper aircraftMapper;

    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private AirlineMapper airlineMapper;

    @Override
    public List<Aircraft> getGratia() {
        return aircraftMapper.getGratia();
    }

    @Override
    public Integer[] getAreaId(String areaName, String areaName2) {
        Integer[] num = new Integer[0];
        try {
            Area area = areaMapper.selectOne(new QueryWrapper<Area>().eq("name", areaName));
            Area area2 = areaMapper.selectOne(new QueryWrapper<Area>().eq("name", areaName2));
            num = new Integer[]{area.getId(), area2.getId()};
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new MyException("444", "没有找到该城市");
        }
        return num;
    }

    @Override
    public Double getMinPrice(String date, Integer areaName1, Integer areaName2) {
        return aircraftMapper.getMinPrice(date, areaName1, areaName2);
    }

    @Override
    public List<Aircraft> getAircraftList(String date, String area1, String area2, List<Time> list, List<Integer> company) {
        try {
            Area area = areaMapper.selectOne(new QueryWrapper<Area>().eq("name", area1));
            Area area0 = areaMapper.selectOne(new QueryWrapper<Area>().eq("name", area2));
            return aircraftMapper.getAircraftList(date, area.getId(), area0.getId(), list, company);
        } catch (NullPointerException e) {
            throw new MyException("444", "城市不存在或航空公司不存在");
        }
    }

    @Override
    public List<String> getCompany(String date) {
        List<String> list=new ArrayList<>();
        List<Aircraft> list2=aircraftMapper.getCompany(date);
        if (list2.size()<=0){
            throw new MyException("444","没有找到当天的航空公司");
        }
        for (int i = 0; i < aircraftMapper.getCompany(date).size(); i++) {
            list.add(airlineMapper.selectById(aircraftMapper.getCompany(date).get(i).getAirlineCompany()).getName());
        }
        return list;
    }

    @Override
    public Integer getSeatLeft(Integer id) {
        return aircraftMapper.getSeatLeft(id);
    }


}
